/*
   This class is to be used to display noBusinessBase/form/WidgetForm in a Dialog.
*/


define(
   [
      'dojo/_base/declare',
      'dojo/_base/lang',
      'dojo/_base/array',
      'dojo/dom-style',
      'dojo/Evented',
      'dojo/topic',
      'noBusinessBase/Dialog'
   ],
   function(declare, lang, dojoArray, domStyle, Evented, topic, Dialog)
   {
      return declare([Dialog, Evented],
      {
         mSignalNameOnClose: null,
         mWidgetForm: null,
         constructor: function(args)
         {
            // Call Super
            dojo.safeMixin(this, args);
            this.mFormClosing = false;
         },
         addWidgetForm: function(pWidgetForm, show)
         {
            this.mWidgetForm = pWidgetForm;
            this.own(
               this.mWidgetForm.on("formExecuted", lang.hitch(this, function(data)
               {
                  if(data && data.close === false)
                     return;

                  this.mFormClosing = true;
                  this.emit("formExecuted", data);
                  this.hide(false);
               })),
               this.mWidgetForm.on("formExecutedButNotSaved", lang.hitch(this, function(data)
               {
                  this.mFormClosing = true;
                  this.emit("formExecutedButNotSaved", data);
                  this.hide(false);
               })),
               this.mWidgetForm.on("formCanceled", lang.hitch(this, function(data)
               {
                  this.mFormClosing = true;
                  this.emit("formCanceled", data);
                  this.hide(false);
               })),
               this.mWidgetForm.on("resize", lang.hitch(this, this.resize)),
               this.mWidgetForm.on("loaded", lang.hitch(this, this.resize)),
               topic.subscribe("UserLoggedOut", lang.hitch(this, function()
               {
                  this.hide(false);
               }))
            );
            
            // Make sure that the scroll bars show up on edit dialogs if the page is too small
            /*
            dojoArray.forEach(this.domNode.children, lang.hitch(this, function(pChildDomNode)
            {
               if(pChildDomNode.className == 'dijitDialogPaneContent')
               {
                  domStyle.set(pChildDomNode, 'overflow', 'auto');
               }
            }));
            */
            
            if(show !== false)
               this.show();

            this.set('content', this.mWidgetForm);
            if(show !== false)
               this.resize();
         },
         resize: function()
         {
            domStyle.set(this.domNode, "left", "10px");
            domStyle.set(this.domNode, "top", "10px");
            this.inherited(arguments);
            domStyle.set(this.domNode, "left", "10px");
            domStyle.set(this.domNode, "top", "10px");
            this._position();
         },
         onHide: function()
         {
            if(!this.mFormClosing)
               this.mWidgetForm.emit('formCanceled');

            this.inherited(arguments);
            this.emit("closed");
            if(this.mSignalNameOnClose)
               topic.publish(this.mSignalNameOnClose);

            this.destroyRecursive();
         }
      });
   }
);
